﻿var CalculatorDvk = function (ddlLocationTypeDvk, ddlNationalDvk, ddlCityDvk
                            , ddlLocationDvk, ddlPartnerDvk, ddlServiceDvk
                            , dteBeginDateDvk, txtPriceAdultsDvk, txtPriceChildsDvk
                            , txtQuantityAdultsDvk, txtQuantityChildDvk, txtVatDvk
                            , txtTotalPriceDvk, txtNoteDvk, seasonTour, tourInfo) {
    $(txtPriceAdultsDvk).val(0);
    $(txtPriceChildsDvk).val(0);
    $(txtQuantityChildDvk).val(0);
    $(txtQuantityAdultsDvk).val(0);
    $(txtTotalPriceDvk).val(0);
    $(txtNoteDvk).val(0);
    $(txtVatDvk).val(0);
    $(ddlCityDvk).html('<option value="-1">--- Chọn tỉnh/thành phố ---</option>');
    $(ddlNationalDvk).html('<option value="-1">--- Chọn quốc gia ---</option>');
    $(ddlLocationDvk).html('<option value="-1">--- Chọn địa điểm ---</option>');
    $(ddlPartnerDvk).html('<option value="-1">--- Chọn nhà cung cấp ---</option>');
    $(ddlServiceDvk).html('<option value="-1">--- Chọn dịch vụ ---</option>');
    //$(ddlNationalDvk).change(loadByCity);
    //load Location
    loadCityService(ddlNationalDvk, ddlCityDvk, ddlPartnerDvk);
    loadLocationByLocationType(ddlLocationTypeDvk, ddlLocationDvk);
    //load thực đơn
    var loadByServiceDvk = function () {
        var typeId = 8;
        var partnerId = $(ddlPartnerDvk).val();
        var locationId = $(ddlLocationDvk).val();
        var season = seasonTour;
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "PriceListByDvk", "TypeId": typeId, "PartnerId": partnerId, "LocationId": locationId, "SeasonTour": season },
            dataType: "json",
            success: function (data) {
                $(ddlServiceDvk).html('<option value="-1">--- Chọn dịch vụ ---</option>');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $(ddlServiceDvk)
                            .append($("<option></option>")
                                .attr("value", item.PriceId)
                                .text(item.ServiceName));
                    });
                }
            }
        });
    };
    $(ddlPartnerDvk).change(loadByServiceDvk);
    $(ddlLocationDvk).change(loadByServiceDvk);

    //load gias dvk
    var loadPricelDvk = function () {
        var serviceId = $(ddlServiceDvk).val();
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "PriceDvk", "ServiceId": serviceId },
            dataType: "json",
            success: function (data) { 
                if (data && data.isError == false) {
                    $(txtPriceAdultsDvk).val(data.PriceAdult);
                    $(txtPriceChildsDvk).val(data.PriceChild);
                    funTongDvk();
                }
            }

        });
    };
    var funTongDvk = function () {
        var priceAdults = $(txtPriceAdultsDvk).val();
        var priceChilds = $(txtPriceChildsDvk).val();
        var quantityAdults = $(txtQuantityAdultsDvk).val();
        var quantityChild = $(txtQuantityChildDvk).val();
        var vat = $(txtVatDvk).val();
        var total = 0;
        if (vat > 0 || vat < 100) {
            total = ((parseInt(priceAdults) * parseInt(quantityAdults)) + (parseInt(priceChilds) * parseInt(quantityChild)));
            total = total + (total * vat / 100);
        }
        $(txtTotalPriceDvk).val(total);
    }; 
    // goi load gia
    $(ddlServiceDvk).change(loadPricelDvk);
    $(txtPriceAdultsDvk).keyup(loadPricelDvk);
    $(txtPriceChildsDvk).keyup(loadPricelDvk);
    $(txtQuantityAdultsDvk).keyup(loadPricelDvk);
    $(txtQuantityChildDvk).keyup(loadPricelDvk);
    $(txtVatDvk).keyup(loadPricelDvk);

    $('#btnInsertDvk').live("click", function () {

        var typeId = 8;
        var tourId = parseInt(tourInfo);
        var dateMenu = $(dteBeginDateDvk).val();
        var menuElementId = $(ddlServiceDvk).val();
        var priceAdults = $(txtPriceAdultsDvk).val();
        var priceChilds = $(txtPriceChildsDvk).val();
        var quantityAdults = $(txtQuantityAdultsDvk).val();
        var quantityChild = $(txtQuantityChildDvk).val();
        var vat = $(txtVatDvk).val();
        if (vat > 0 && vat <= 100) { } else { vat = 0; }
        var calculatorId = $(this).attr("calculatorid");
        var className = $(this).attr("class");
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: {
                "function": "InsertDvk", "TypeId": typeId, "TourId": tourId, "menuElementID": menuElementId,
                "PriceAdults": priceAdults, "PriceChilds": priceChilds, "Date": dateMenu,
                "CalculatorId": calculatorId, "ClassName": className, 'Vat': vat,
                'QuantityAdults': quantityAdults, 'QuantityChild': quantityChild
            },
            dataType: "json",
            success: function (data) {

                $(".ListDvk").remove();
                $(ddlLocationTypeDvk).val('-1');
                $(ddlNationalDvk).val('-1');
                $(ddlCityDvk).val('-1');
                $(ddlLocationDvk).val('-1');
                $(ddlPartnerDvk).val('-1');
                $(ddlServiceDvk).val('-1');
                $(dteBeginDateDvk).val('');
                $(txtNoteDvk).val('');
                $(txtTotalPriceDvk).val('0');
                $(txtVatDvk).val('0');
                $(txtQuantityChildDvk).val('0');
                $(txtQuantityAdultsDvk).val('0');
                $(txtPriceAdultsDvk).val('0');

                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        var totaIitem = (parseInt(item.PriceAdults) * parseInt(item.QuantityAdults)) + (parseInt(item.PriceChild) * parseInt(item.QuantityChild));
                        totaIitem = totaIitem + (totaIitem * item.Vat / 100);
                        $('#new-Dvk')
                            .before($("<tr class='ListDvk' id='Dvk-" + item.CalculatorId + "'></tr>").html('<td></td>' +
                                '<td >' + item.LocationTypeName + '</td>' +
                                '<td >' + item.NationalName + '</td>' +
                                '<td >' + item.CityName + '</td>' +
                                '<td >' + item.LocationName + '</td>' +
                                '<td >' + item.PartnerName + '</td>' +
                                '<td >' + item.ServiceName + '</td>' +
                                '<td >' + item.DateBegin + '</td>' +
                                '<td >' + item.PriceAdults + '</td>' +
                                '<td >' + item.PriceChild + '</td>' +
                                '<td >' + item.QuantityAdults + '</td>' +
                                '<td >' + item.QuantityChild + '</td>' +
                                '<td >' + item.Vat + '</td>' +
                                '<td >' + totaIitem + '</td>' +
                                '<td></td>' +
                                '<td>' +
                                '<input class="btnUpdateDvk"  type="button" value="Cập nhật" calculatorid="' + item.CalculatorId + '"   />' +
                                '<input class="btnDeleteDvk"  type="button" value="Xóa" calculatorid="' + item.CalculatorId + '"   />' +
                                '</td>'));
                    });
                    $('#TotalDvkPrices').html(parseInt(data.TotalDvkChild) + parseInt(data.TotalDvkAdults));
                    $('#TotalDvkAdults').html(data.TotalDvkAdults);
                    $('#TotalDvkChild').html(data.TotalDvkChild);

                    $('#btnInsertDvk').attr("calculatorid", "-1").val("Thêm");
                    $('.btnDeleteDvk').show();
                    $('#btnDeleteDvk').hide();
                }

            }
        });

    });
    $('.btnUpdateDvk').live("click", function () { 
        var calculatorId = $(this).attr("calculatorid"); 
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "UpdateDvk", "CalculatorId": calculatorId },
            dataType: "json",
            success: function (data) {
               
                $('#Dvk-' + calculatorId).remove();
                $(ddlLocationTypeDvk).val(data.LocationTypeId);
                $(ddlNationalDvk).val(data.NationalId);
                loadCityByService(data.NationalId, data.PartnerId, data.CityId, ddlCityDvk, ddlPartnerDvk,
                    data.LocationTypeId, ddlLocationTypeDvk, data.LocationId,   data.ServiceId);
              
                $(dteBeginDateDvk).val(data.DateBegin);
                $(txtTotalPriceDvk).val(parseInt( data.TotalPriceDvkAdults )+ parseInt(data.TotalPriceDvkChild));
                $(txtVatDvk).val(data.Vat);
                $(txtQuantityChildDvk).val(data.QuantityChild);
                $(txtQuantityAdultsDvk).val(data.QuantityAdults);
                $(txtPriceAdultsDvk).val(data.PriceAdults);
                $(txtPriceChildsDvk).val(data.PriceChild);
                $(txtNoteDvk).val("");
                $('#btnInsertDvk').attr("calculatorid", calculatorId).val("Lưu");
                $('.btnUpdateDvk').hide();
                $('#btnDeleteDvk').show();
            }
        });
    });
    $('.btnDeleteDvk').live("click", function () {
        var typeId = 8;
        var tourId = parseInt(tourInfo);
        var dateMenu = $(dteBeginDateDvk).val();
        var menuElementId = $(ddlServiceDvk).val();
        var priceAdults = $(txtPriceAdultsDvk).val();
        var priceChilds = $(txtPriceChildsDvk).val();
        var quantityAdults = $(txtQuantityAdultsDvk).val();
        var quantityChild = $(txtQuantityChildDvk).val();
        var vat = $(txtVatDvk).val();
        if (vat > 0 && vat <= 100) { } else { vat = 0; }
        var calculatorId = $(this).attr("calculatorid");
        var className = $(this).attr("class");
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: {
                "function": "InsertDvk", "TypeId": typeId, "TourId": tourId, "menuElementID": menuElementId,
                "PriceAdults": priceAdults, "PriceChilds": priceChilds, "Date": dateMenu,
                "CalculatorId": calculatorId, "ClassName": className, 'Vat': vat,
                'QuantityAdults': quantityAdults, 'QuantityChild': quantityChild
            },
            dataType: "json",
            success: function (data) {
                $(".ListDvk").remove();
                $(ddlLocationTypeDvk).val('-1');
                $(ddlNationalDvk).val('-1');
                $(ddlCityDvk).val('-1');
                $(ddlLocationDvk).val('-1');
                $(ddlPartnerDvk).val('-1');
                $(ddlServiceDvk).val('-1');
                $(dteBeginDateDvk).val('');
                $(txtNoteDvk).val('');
                $(txtTotalPriceDvk).val('0');
                $(txtVatDvk).val('0');
                $(txtQuantityChildDvk).val('0');
                $(txtQuantityAdultsDvk).val('0');
                $(txtPriceAdultsDvk).val('0');

                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        var totaIitem = (parseInt(item.PriceAdults) * parseInt(item.QuantityAdults)) + (parseInt(item.PriceChild) * parseInt(item.QuantityChild));
                        totaIitem = totaIitem + (totaIitem * item.Vat / 100);
                        $('#new-Dvk')
                            .before($("<tr class='ListDvk' id='Dvk-" + item.CalculatorId + "'></tr>").html('<td></td>' +
                                '<td >' + item.LocationTypeName + '</td>' +
                                '<td >' + item.NationalName + '</td>' +
                                '<td >' + item.CityName + '</td>' +
                                '<td >' + item.LocationName + '</td>' +
                                '<td >' + item.PartnerName + '</td>' +
                                '<td >' + item.ServiceName + '</td>' +
                                '<td >' + item.DateBegin + '</td>' +
                                '<td >' + item.PriceAdults + '</td>' +
                                '<td >' + item.PriceChild + '</td>' +
                                '<td >' + item.QuantityAdults + '</td>' +
                                '<td >' + item.QuantityChild + '</td>' +
                                '<td >' + item.Vat + '</td>' +
                                '<td >' + totaIitem + '</td>' +
                                '<td></td>' +
                                '<td>' +
                                '<input class="btnUpdateDvk"  type="button" value="Cập nhật" calculatorid="' + item.CalculatorId + '"   />' +
                                '<input class="btnDeleteDvk"  type="button" value="Xóa" calculatorid="' + item.CalculatorId + '"   />' +
                                '</td>'));
                    });
                    $('#TotalDvkPrices').html(parseInt(data.TotalDvkChild) + parseInt(data.TotalDvkAdults));
                    $('#TotalDvkAdults').html(data.TotalDvkAdults);
                    $('#TotalDvkChild').html(data.TotalDvkChild);

                    $('#btnInsertDvk').attr("calculatorid", "-1").val("Thêm");
                    $('.btnDeleteDvk').show();
                    $('#btnDeleteDvk').hide();

                }

            }
        });
    }); 

    var loadCityByService = function (nationalId, partnerId, cityId, provinceElement, partnerElement, locationTypeId, ddlLocationTypeDvk, LocationId,  serviceId) {
      
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "ListCityandConcernByNational", "NationalId": nationalId },
            dataType: "json",
            success: function (data) {
                $(provinceElement).html('<option value="-1"> --- Chọn tỉnh/thành phố--- </option>');
                $(partnerElement).html('<option value="-1"> --- Chọn nhà cung cấp--- </option>');
                if (data && data.isError == false) {
                    $.each(data.arrayCity, function (index, item) {
                        $(provinceElement)
                            .append($("<option></option>")
                                .attr("value", item.IDCity)
                                .text(item.NameCity));
                    });

                    $.each(data.arrayPartner, function (index, item) {
                        $(partnerElement)
                            .append($("<option></option>")
                                .attr("value", item.IDPartner)
                                .text(item.NamePartner));
                    });
                }
                $(ddlCityDvk).val(cityId);
                $(ddlPartnerDvk).val(partnerId);
                loadLocationByType(locationTypeId, ddlLocationTypeDvk, LocationId,    serviceId);
            }
        });



    };
    var loadLocationByType = function (locationTypeId, ddlLocationTypeDvk, LocationId,     serviceId) {
     
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "ListLocationByLocationType", "LocationType": locationTypeId },
            dataType: "json",
            success: function (data) {
                $(ddlLocationDvk).html('<option value="-1"> ---Chọn địa điểm--- </option>');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $(ddlLocationDvk)
                            .append($("<option></option>")
                                .attr("value", item.ID)
                                .text(item.Name));
                    });
                }
                $(ddlLocationDvk).val(LocationId);
                loadByService(   serviceId);
            }
        });
    };
    var loadByService = function (  serviceId) {
      
        var typeId = 8;
        var partnerId = $(ddlPartnerDvk).val();
        var locationId = $(ddlLocationDvk).val();
        var season = seasonTour;
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "PriceListByDvk", "TypeId": typeId, "PartnerId": partnerId, "LocationId": locationId, "SeasonTour": season },
            dataType: "json",
            success: function (data) {
                $(ddlServiceDvk).html('<option value="-1">--- Chọn dịch vụ ---</option>');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $(ddlServiceDvk)
                            .append($("<option></option>")
                                .attr("value", item.PriceId)
                                .text(item.ServiceName));
                    });
                    $(ddlServiceDvk).val(serviceId);
                }
               
            }
        });
    };
};

